package com.eascs.p2p.admin.service.impl;

import java.util.List;

import javax.inject.Inject;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.app.dao.PageResult;
import com.eascs.p2p.admin.manager.IAdminUserManager;
import com.eascs.p2p.common.entity.AdminRoleEntity;
import com.eascs.p2p.common.entity.AdminUserEntity;
import com.eascs.p2p.common.entity.AdminUserRoleEntity;
import com.eascs.p2p.common.service.IAdminUserService;
import com.eascs.p2p.common.vo.AdminUserVO;
import com.eascs.p2p.common.vo.ModelResult;

@Service
public class AdminUserServiceImpl implements IAdminUserService {
	@Inject
	IAdminUserManager adminUserManager;

	@Override
	public ModelResult<AdminUserEntity> queryAdminUserByLogin(String account, String password) {

		ModelResult<AdminUserEntity> result = new ModelResult<AdminUserEntity>();
		try {
			result.setModel(adminUserManager.queryAdminUserByLogin(account, password));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("后台用户登录：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<AdminUserEntity> queryAdminUserById(String id) {

		ModelResult<AdminUserEntity> result = new ModelResult<AdminUserEntity>();
		try {
			result.setModel(adminUserManager.queryAdminUserById(id));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("根据id查询用户：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<Boolean> updateAdminUser(AdminUserEntity admin) {

		ModelResult<Boolean> result = new ModelResult<Boolean>();
		try {
			result.setModel(adminUserManager.updateAdminUser(admin));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("更新用户：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<PageResult<Object[]>> findPageAdminUserEntity(AdminUserVO vo) {
		ModelResult<PageResult<Object[]>> result = new ModelResult<PageResult<Object[]>>();
		try {
			result.setModel(adminUserManager.findPageAdminUserEntity(vo));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("分页查询用户：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<String> saveAdminUser(AdminUserEntity admin) {
		ModelResult<String> result = new ModelResult<String>();
		try {
			result.setModel(adminUserManager.saveAdminUser(admin));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("保存用户：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<List<AdminRoleEntity>> queryRoleList(){
		ModelResult<List<AdminRoleEntity>> result = new ModelResult<List<AdminRoleEntity>>();
		try {
			result.setModel(adminUserManager.queryRoleList());
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("查询角色列表：", e.getMessage());
		}
		return result;
		
	}
	
	@Override
	public ModelResult<PageResult<Object[]>> queryRoleListByUser(AdminUserVO vo){
		ModelResult<PageResult<Object[]>> result = new ModelResult<PageResult<Object[]>>();
		try {
			result.setModel(adminUserManager.queryRoleListByUser(vo));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("查询角色列表：", e.getMessage());
		}
		return result;
		
	}
	
	
	@Override
	public ModelResult<Boolean> isExistUserRole(String userId,String roleId){
		ModelResult<Boolean> result = new ModelResult<Boolean>();
		try {
			result.setModel(adminUserManager.isExistUserRole(userId,roleId));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("查询角色列表：", e.getMessage());
		}
		return result;
	}
	@Override
	public ModelResult<String> saveUserRole(AdminUserRoleEntity adminUserRoleEntity){
		ModelResult<String> result = new ModelResult<String>();
		try {
			result.setModel(adminUserManager.saveUserRole(adminUserRoleEntity));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("保存用户的角色信息：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<Boolean> delUserRole(String userId, String roleId) {
		ModelResult<Boolean> result = new ModelResult<Boolean>();
		try {
			result.setModel(adminUserManager.delUserRole(userId,roleId));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("删除用户的角色信息：", e.getMessage());
		}
		return result;
	}
	/*@Override
	public <T extends User> T getUserByAccount(String loginName, Class<T> type){
		return adminUserManager.getUserByAccount(loginName,type);
	}*/
	
	@Override
	public AdminUserEntity getUserByAccount(String loginName){
		return adminUserManager.getUserByAccount(loginName);
	}

	@Override
	public ModelResult<Boolean> checkIsAdmin(String userId) {
		ModelResult<Boolean> modelResult=new ModelResult<>();
		try {
			modelResult.withModel(adminUserManager.checkIsAdmin(userId));
		} catch (Exception e) {
			modelResult.withError("AdminUserServiceImpl.checkIsAdmin", "验证是否超级管理员失败:"+e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}

	@Override
	public ModelResult<Boolean> checkAuth(String userId, String roleCode) {
		ModelResult<Boolean> modelResult=new ModelResult<>();
		try {
			modelResult.withModel(adminUserManager.checkUserRole(userId, roleCode));
		} catch (Exception e) {
			 modelResult.withError("AdminUserServiceImpl.checkAuth", "验证是否用户关闭权限失败:"+e.getMessage());
			e.printStackTrace();
		}
		return modelResult;
	}
	
}
